package com.tbvtc.agriculturalcreditchainserver2025.utils.Pest;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.tbvtc.agriculturalcreditchainserver2025.pojo.pear.MainPestPear;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Component
public class PestUtils {

    @Resource
    private PestHttpUtils pestHttpUtils;
    //摄像头
    public String getCameraImage(){
        String captureImage = pestHttpUtils.getCaptureImage(PestConstant.quality, PestConstant.width);
        JsonObject jsonObject = JsonParser.parseString(captureImage).getAsJsonObject();
        String url = jsonObject.get(PestConstant.url).getAsString();
        String lastPart = StringUtils.substringAfterLast(url, "/");
         return PestConstant.filePath + lastPart;
    }

    /**
     * 获取图像识别结果
     * @param hash
     * @return
     */
    public String pestResult(String hash) {
        byte[] bytes = new RestTemplate().getForObject(PestConstant.ipfsURL + hash, byte[].class);
        return pestHttpUtils.uploadImage(bytes, PestConstant.fileName);//获取图像识别结果
    }



}

